package com.mz.ozy.activity.find;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.ArticleTypeBean;
import com.mz.ozy.bean.ArticleTypeListBean;
import com.mz.ozy.mvp.presenter.StrategyTypePresenter;
import com.mz.ozy.mvp.view.StrategyTypeContract;
import com.mz.ozy.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

public class WholeStrategyActivity extends BaseActivity<StrategyTypeContract.View, StrategyTypePresenter> implements StrategyTypeContract.View, ViewPager.OnPageChangeListener {
    private SlidingTabLayout mTabLayout;
    private ViewPager mViewPager;
    private EditText etSearch;
    private InputMethodManager manager;
    private ArrayList<StrategyFragmet> fragments = new ArrayList<>();
    private StrategyFragmet mCurFragment;
    private String[] titles = new String[]{"商学院", "公司资讯", "产品资讯", "行业资讯"};
    private boolean isLoadSuccess = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_whole_strategy);
//        initTitle();
//        tvTitle.setText("所有攻略");
        titleBar = findViewById(R.id.titleBar);
        manager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        setTitleBarMargin();
        etSearch = findViewById(R.id.et_search);
        initView();
    }

    @Override
    public void onBackPressed() {
        MainActivity.tabHost.setCurrentTab(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isLoadSuccess) {
            requestDataForNetwork();
        }
    }

    private void initView() {
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mCurFragment != null) {
                    mCurFragment.searchArticle(s.toString(), false);
                }
            }
        });
    }

    /**
     * 设置沉浸式状态栏时，要把标题栏往下移动状态栏的高度
     */
    protected void setTitleBarMargin() {
        int length = AppUtils.getStatusBarHeight(this);
        //注意布局是哪种Layout
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(titleBar.getLayoutParams());
        lp.setMargins(0, length, 0, 0);
        titleBar.setLayoutParams(lp);
    }

    private void requestDataForNetwork() {
        mPresenter.queryArticleType(this);
    }

    private void initViewPager(ArticleTypeListBean listBean) {
        mViewPager = findViewById(R.id.vp_main_content);
        List<ArticleTypeBean> list = listBean.getList();
        for (int i = 0; i < list.size(); i++) {
            //创建Fragment
            StrategyFragmet strategyFragmet = StrategyFragmet.newInstance(i, list.get(i).getId() + "");
            fragments.add(strategyFragmet);
        }
        mCurFragment = fragments.get(0);
        //设置适配器
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position).getTypeName();
            }
        });
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(this);
    }


    private void initTabLayout(ArticleTypeListBean listBean) {
        mTabLayout = findViewById(R.id.vpti_main_tab);
        mTabLayout.setViewPager(mViewPager);
        mTabLayout.getTitleView(0).setTypeface(Typeface.DEFAULT_BOLD);
//        int hspace = getResources().getDimensionPixelSize(R.dimen.dp_15);
//        IndicatorLineUtils.setTabWidth(mTabLayout, hspace);


        List<ArticleTypeBean> list = listBean.getList();


        for (int i = 0; i < list.size(); i++) {
            mTabLayout.getTitleView(i).setText(list.get(i).getTypeName());
        }


//        for (int i = 0; i < titles.length; i++) {
//            mTabLayout.getTitleView(i).setText(titles[i]);
//        }
    }

    @Override
    public void getArticleType(ArticleTypeListBean listBean) {
        isLoadSuccess = true;
        initViewPager(listBean);
        initTabLayout(listBean);
//        mTabLayout.removeAllTabs();
//        List<ArticleTypeBean> list = listBean.getPageItems();
//        int hspace = getResources().getDimensionPixelSize(R.dimen.dp_15);
//        IndicatorLineUtils.setTabWidth(mTabLayout,hspace);
//        for (int i = 0; i < list.size(); i++) {
//            ArticleTypeBean articleTypeBean = list.get(i);
//            mTabLayout.getTabAt(i).setText(articleTypeBean.getTypeName());
//        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mCurFragment = fragments.get(position);
        final String searchMsg = etSearch.getText().toString().trim();
        mCurFragment.searchArticle(searchMsg, true);
        if (position != 0) {
            mTabLayout.getTitleView(0).setTypeface(Typeface.DEFAULT);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
